//
//  returnMonryDetial.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/22.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "returnMonryDetial.h"
#import "detialHeadCell.h"
#import "returnMoneyDetialCell.h"
#import "historyModel.h"
@interface returnMonryDetial () {
    historyModel *headModel;
}
@property (nonatomic, strong)NSMutableArray *cellList;
@end

@implementation returnMonryDetial

- (void)viewDidLoad {
    self.title = @"还款明细";
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin);
    self.tableView = [[UITableView alloc]initWithFrame:frame];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //        if (iPhoneX) {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 88, 0);
        //        }else {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        //        }
        //        self.detialView.scrollIndicatorInsets = self.detialView.contentInset;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    [self addRefreshHeader];
    [self confingureTableView];
    [self huankuanDetialRequest];
}

// MARK: - 下拉刷新
- (void)addRefreshHeader {
    [super addRefreshHeader];
}

- (void)confingureTableView {
    // [super confingureTableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([detialHeadCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([detialHeadCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([returnMoneyDetialCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([returnMoneyDetialCell class])];
}

#pragma mark --- uitableViewDelegate and dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return _cellList.count;
    }
    return headModel ? 1 : 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 115;
    }
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        detialHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([detialHeadCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = headModel;
        return cell;
    }
    returnMoneyDetialCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([returnMoneyDetialCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.cellList objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView  *grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
    grayView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return grayView;
}

- (void)huankuanDetialRequest {
    NSDictionary *dic = @{
                          @"token":USERTOKEN
                          };
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@%@", [[apiManger shareApiManger] huanKuanDetial], self.ID];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            headModel = [historyModel mj_objectWithKeyValues:responseObject[@"data"]];
            weakSelf.cellList = [historyModel mj_objectArrayWithKeyValuesArray:responseObject[@"pager"][@"result"]];
            [weakSelf.tableView reloadData];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (NSMutableArray *)cellList {
    if (!_cellList) {
        _cellList = [NSMutableArray array];
    }
    return _cellList;
}
@end
